package cn.edu.neu.lab603.cloudeval.strategy

import java.util.concurrent.atomic.AtomicLong

import cn.edu.neu.lab603.cloudeval.entity.{DataCenter, PhysicalMachine => Host, VirtualMachine => VM}

/** 提前结束的调度策略。
  * 在检测间隔中，如果该主机没有被使用，则将其关闭。
  *
  * Created by yfwz100 on 2016/11/10.
  */
class EarlyStoppingReallocationStrategy(checkPeriod: Int = 25) extends ReallocationStrategy {

  import policy.MigrationDef._

  // 时钟周期计数器
  val clockCounter = new AtomicLong

  override def checkResourcesViolation(dataCenter: DataCenter): Boolean = {
    if (clockCounter.incrementAndGet() % checkPeriod == 0)
      dataCenter.getRunningHost.exists(_.isIdle)
    else
      false
  }

  override def reallocate(dataCenter: DataCenter): scala.collection.Map[VM, Host] = Map.empty

  override def cleanup(dataCenter: DataCenter): Unit = {
    dataCenter.getRunningHost.filter(_.isIdle).foreach(_.shutdown())
  }

  override def toString: String = "early-stopping-naive"

}
